Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Matching the character "}"

    Hello,

    I would like to use the split command with the character "}" as a delimiter inside a forvalues loop, but Stata interprets the character "}" as the closing curly bracket of the loop.

    Here is the code I would like to run

    forvalues yr=1980/2020 {

    import delimited using "rawfile_`yr'.txt", delimiters("\t") clear stripquotes(no)
    split var, p(`"}],""') limit(1) notrim
    keep var1
    rename var1 symbol
    save symbol_`yr', replace
    }

    I have tried inserting a backslash before the "{" in the split command. I did it like this

    split var, p(`"\}],""') limit(1) notrim

    I was hoping that Stata would interpret the backslash as an escape character to avoid the "}" be interpreted as a regular-loop operator. It did work. However, the split command now misinterprets the delimiter to be used. It uses the delimiter "\}]," including a backslash which does not exist in the string variable var.

    Do you know any fix for that?

    Thank you so much for your help,

    O.





  • #2
    This seems to accomplish what you seek.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 var23
    "test"   
    "one}two"
    "one]two"
    "one,two"
    end
    forvalues i=23/23 {
        split var`i', parse( `"}"' ] , ) limit(1) notrim
        }
    list
    Code:
    . list
    
         +------------------+
         |   var23   var231 |
         |------------------|
      1. |    test     test |
      2. | one}two      one |
      3. | one]two      one |
      4. | one,two      one |
         +------------------+

    Comment


    • #3
      Hi William,

      Thank you so much for your reply! Sorry for my slow reactivity to your message.

      I have copy-pasted your code in my do-file editor (see picture below), and as you can see the problem I was describing persists. The do-file editor wrongly interprets the character "}" as the closing curly bracket of the loop. I really do not know how to fix that.

      Best,

      O.

      Click image for larger version

Name:	do_file_picture.png
Views:	1
Size:	145.3 KB
ID:	1556458

      Comment


      • #4
        Hi William,

        Please note that your code works properly (even though the display in the do-file editor is incorrect). In my code, the loop breaks at the split command. I am going to work on it. I keep you informed.

        Best,

        Olivier

        Comment


        • #5
          William Lisowski 's code correctly parses the variable.

          Yet, (I believe) olivier has asked a different question - it is about Stata's doeditor that get's confused when parsing the source code.
          Indeed the screenshot shows the incorrect code folding on the left margin.

          In order not to confuse the Stata's doeditor (Scintilla) avoid mentioning the bracket directly, but rather as an ASCII character code:
          Code:
          forvalues i=23/23 {
              split var`i', parse( `"`=char(175)'"' ] , ) limit(1) notrim
          }
          and the code will fold correctly then. Notably, the code will work regardless, and it is just a decorative presentation in the do-editor that will change.

          ​​​​​​​This is not the only case when the Stata's do-editor gets confused with the syntax I believe, but in most cases its behavior is quite reasonable.

          Olivier could have been more precise in describing what the problem is.
          PS: Stata doesn't have the notion of escaping special characters with \ slash like it is common in some other languages, like C#. But it does have that for escaping the slash itself when parsing a macro, and, perhaps in some other cases. But don't expect \n to work as a new line or \t as a tab character.

          Comment

          Working...
          X